from comman.functions import *


class Relu:
    def __init__(self):
        self.mask = None

    def forward(self, x):
        self.mask = (x <= 0)
        # 防止x被修改
        out = x.copy()
        out[self.mask] = 0
        return out

    def backward(self, dout):
        dout[self.mask] = 0
        return dout


class Sigmoid:
    def __init__(self):
        self.out = None

    def forward(self, x):
        out = sigmoid(x)
        self.out = out
        return out

    def backward(self, dout):
        dx = dout * (1.0 - self.out) * self.out

        return dx


class Softmax:
    def __init__(self):
        self.y = None  # softmax的输出

    def forward(self, x):
        self.y = softmax(x)
        return self.y

    def backward(self, dout):
        return dout


activator_dict = {"relu": Relu, "sigmoid": Sigmoid, "softmax": Softmax}
